<?php
/**
 * @package orinoco.framework
 * @subpackage framework
 * @author Ryan Yonzon <rawswift@gmail.com>
 * @copyright Copyright 2008 Ryan Yonzon 
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * view/template helper
 */
class coreHelper {

	/**
	 * construct hyperlink
	 *
	 * @param string $_title link name
	 * @param string $_action action name
	 * @param string $_controller controller name
	 * @param array $_additional_parameters additional parameters, e.g. ?id=1&name=foo&add=bar
	 */
	public function link_to($_title = NULL, $_action = NULL, $_controller = NULL, $_additional_parameters = NULL) {

		// controller
		if(isset($_controller) && $_controller != '') {
			$_to_controller = $_controller;
		} else {
			$_to_controller = $this->_controller;
		}					

		if(USE_TRAILING_SLASH) {
			$_to_controller = $_to_controller . '/';
		}
		
		// method
		$_add_param = NULL;		
		if(isset($_action) && $_action != '') {
				if(USE_TRAILING_SLASH) {
					$_to_action = $_action;					
					$_to_action = $_to_action . '/';
				} else {
					$_to_action = '/' . $_action;
				}

				// additional parameters
				if(isset($_additional_parameters)) {
					$_add_param = '?';
					foreach($_additional_parameters as $_k => $_v) {
						$_add_param = $_add_param . $_k . '=' . $_v . '&';
					}		
					// remove trailing "&"
					$_add_param[strlen($_add_param) - 1] = '';
					$_add_param = trim($_add_param);
				}
			
		} else {
			$_to_action = NULL;			
		}
		
			// if title is NULL/empty then set default title
			if(!isset($_title)) {
				$_title = 'Click Here';
			}

		$_hyperlink = '<a href="' . '/' . $_to_controller . $_to_action . $_add_param . '">' . $_title . '</a>';

		// let it boggie!
		print $_hyperlink;
				
	}
	
	/**
	 * convert permalink/URI format to word format 
	 *
	 * @example foo-bar --> foo bar 
	 * @param string $_permalink string to convert
	 * @return string in word format
	 */
	public function to_word($_permalink) {
		$_patterns = array('(;)', '(\+)', '(-)');
		$_replacements = array('-', '\'', ' ');
		return preg_replace($_patterns, $_replacements, $_permalink);
	}

	/**
	 * string to permalink format
	 *
	 * @example foo bar --> foo-bar 
	 * @param string $_string
	 * @return string in permalink format
	 */
	public function to_permalink($_string) {
		$_patterns = array('(-)', '(\')', '( )');
		$_replacements = array(';', '+', '-');
		return preg_replace($_patterns, $_replacements, $_string);
	}
	
	/**
	 * create HTML form tag
	 *
	 * @param string $_action action name
	 * @param string $_controller controller name
	 * @param string $_method method type, get or post 
	 * @param array $_additional_parameters additional parameters, e.g. ?id=1&name=foo&add=bar
	 * @todo see how we can improve this method 
	 */
	public function start_form_tag($_action, $_controller = NULL, $_method = NULL, $_additional_parameters = NULL) {

		$_mapped_uri = new Router();
		$_id_name = 'form_' . $_mapped_uri->_controller . '_' . $_mapped_uri->_method;

		if(!isset($_controller)) {
			$_controller = $_mapped_uri->_controller;
		}

		$_point_to_action = '/' . $_controller . '/' . $_action;
		
			if(isset($_mapped_uri->_id)) {
				$_point_to_action = $_point_to_action . '/' . $_mapped_uri->_id;
			}

		if(USE_TRAILING_SLASH) {
			$_point_to_action = $_point_to_action . '/';
		}
		
		// additional parameters
		if(isset($_additional_parameters)) {
			$_add_param = '?';
			foreach($_additional_parameters as $_k => $_v) {
				$_add_param = $_add_param . $_k . '=' . $_v . '&';
			}		
			// remove trailing "&"
			$_add_param[strlen($_add_param) - 1] = '';
			$_add_param = trim($_add_param);
			$_point_to_action = $_point_to_action . $_add_param;
		}
		
		if(isset($_method) && $_method != '') {
			$_method_to_use = $_method;
		} else {
			$_method_to_use = 'post';
		}
		
		$_form_tag = '<form id="' . $_id_name . '" method="' . $_method_to_use . '" action="' . $_point_to_action . '">';
		
		print $_form_tag;
		
	}

	/**
	 * create HTML end from tag
	 */
	public function end_form_tag() {
		print '</form>';			
	}

	/**
	 * create a button tag
	 *
	 * @param string $_value button value/name
	 * @param string $_type button type
	 * @todo add additional_parameter support and see how we can improve this method
	 */
	public function button_tag($_value = 'Submit', $_type = 'submit') {
		$_attribute = NULL;
		$_button_name = 'button_' . $this->_controller . '_' . $this->_method;
		$_id_name = $_button_name;
		
		// check for set or specified attributes
		if(isset($_array_attributes)) {
			foreach($_array_attributes as $_k => $_v) {
				if($_k == 'name') {
					$_input_name = $_v;
				} else if($_k == 'id') {
					$_id_name = $_v;
				} else if($_k == 'value') {
					$_current_value = $_v;
					$_check_for_value = false;					
				} else {
					$_attribute = $_attribute . ' ' . $_k . '="' . $_v . '"';
				}
			}		
		}		
		
		print '<input id="' . $_id_name . '" name="' . $_button_name . '" type="' . $_type . '" value="' . $_value . '" ' . $_attribute . ' />';		
	}
	
	/**
	 * print an array
	 *
	 * @param array $_arr array to print
	 */
	public function debug_array($_arr) {
		echo '<pre>';
		print_r($_arr);
		echo '</pre>';		
	}	

} // end class

// -EOF-